package com.haulmont.asu.entity;

import com.haulmont.chile.core.annotations.Composition;
import com.haulmont.chile.core.annotations.MetaProperty;
import com.haulmont.chile.core.annotations.NamePattern;
import com.haulmont.cuba.core.entity.StandardEntity;
import com.haulmont.cuba.core.global.AppBeans;
import com.haulmont.cuba.core.global.Messages;
import com.haulmont.cuba.security.entity.User;

import javax.persistence.*;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * Author: Sergey42
 * Date: 17.11.13 13:32
 */
@Table(name = "EXT_COMMAND")
@Entity(name = "ext$Command")
@Inheritance(strategy = InheritanceType.JOINED)
@DiscriminatorColumn(name = "COMMAND_TYPE", discriminatorType = DiscriminatorType.INTEGER)
@DiscriminatorValue("0")
@NamePattern("%s|commandName")
public class Command extends StandardEntity {

    private static final long serialVersionUID = -1885774199460085002L;

    @Column(name = "NUMBER", length = 10)
    protected String number;

    @ManyToOne
    @JoinColumn(name = "COMMAND_TYPE_ID")
    protected CommandType commandType;

    @ManyToOne
    @JoinColumn(name = "CREATOR_ID")
    protected User creator;

    @Column(name = "CREATE_DATE")
    @Temporal(TemporalType.DATE)
    protected Date createDate;

    @ManyToOne
    @JoinColumn(name = "CERTIFICATE_USER_ID")
    protected User certificateUser;

    @Column(name = "CERTIFICATION_DATE")
    @Temporal(TemporalType.DATE)
    protected Date certificationDate;

    @Column(name = "COMMAND_TEXT", length = 1000)
    protected String commandText;

    @OneToMany(mappedBy = "command")
    @Composition
    protected List<CommandRelation> commandRelations;

    @Column(name = "TEMPLATE")
    protected Boolean template = Boolean.FALSE;

    @ManyToOne
    @JoinColumn(name = "PARENT_COMMAND_ID")
    protected Command parentCommand;

    @MetaProperty
    public String getCommandName() {
        Messages messages = AppBeans.get(Messages.class);
        StringBuilder sb = new StringBuilder();
        sb.append(commandType != null ? commandType.getName() : "");
        sb.append(" ");
        if (number != null) {
            sb.append(messages.getMessage(Command.class, "num"));
            sb.append(" ");
            sb.append(number);
        }
        sb.append(" ");
        sb.append(messages.getMessage(Command.class, "from"));
        sb.append(" ");
        sb.append(new SimpleDateFormat("dd.MM.yyyy").format(createDate));
        return sb.toString();
    }

    public CommandType getCommandType() {
        return commandType;
    }

    public void setCommandType(CommandType commandType) {
        this.commandType = commandType;
    }

    public User getCreator() {
        return creator;
    }

    public void setCreator(User creator) {
        this.creator = creator;
    }

    public Date getCreateDate() {
        return createDate;
    }

    public void setCreateDate(Date createDate) {
        this.createDate = createDate;
    }

    public User getCertificateUser() {
        return certificateUser;
    }

    public void setCertificateUser(User certificateUser) {
        this.certificateUser = certificateUser;
    }

    public Date getCertificationDate() {
        return certificationDate;
    }

    public void setCertificationDate(Date certificationDate) {
        this.certificationDate = certificationDate;
    }

    public String getCommandText() {
        return commandText;
    }

    public void setCommandText(String commandText) {
        this.commandText = commandText;
    }

    public String getNumber() {
        return number;
    }

    public void setNumber(String number) {
        this.number = number;
    }

    public List<CommandRelation> getCommandRelations() {
        return commandRelations;
    }

    public void setCommandRelations(List<CommandRelation> commandRelations) {
        this.commandRelations = commandRelations;
    }

    public Boolean getTemplate() {
        return template;
    }

    public void setTemplate(Boolean template) {
        this.template = template;
    }

    public Command getParentCommand() {
        return parentCommand;
    }

    public void setParentCommand(Command parentCommand) {
        this.parentCommand = parentCommand;
    }
}
